package com.example.csmall.product.controller;

import com.example.csmall.product.pojo.dto.CategoryAddNewDTO;
import com.example.csmall.product.pojo.vo.CategoryListByParentIdVO;
import com.example.csmall.product.pojo.vo.CategoryListItemVO;
import com.example.csmall.product.service.CategoryService;
import com.example.csmall.product.web.JsonResult;
import com.example.csmall.product.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/categorys")
@Slf4j
@Api(tags = "05 分类 ")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @PostMapping("/add-new")
    @ApiOperation("添加类别")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO) {
        categoryService.addNew(categoryAddNewDTO);
        JsonResult jr = new JsonResult();
        jr.setState(ServiceCode.OK.getValue());
        jr.setMessage("类别添加成功");
        return jr;
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除分类")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "删除分类的ID",dataType = "long")
    public JsonResult<Void> deleteById(@Range(max = 150,min = 0,message = "删除相册失败,id无效") @PathVariable Long id){
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用分类")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用分类")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/start-display")
    @ApiOperation("启用导航")
    public JsonResult<Void> startDisplay(@PathVariable Long id) {
        categoryService.startDisplay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/forbidden-display")
    @ApiOperation("禁用导航")
    public JsonResult<Void> forbiddenDisplay(@PathVariable Long id) {
        categoryService.forbiddenDisplay(id);
        return JsonResult.ok();
    }

    @GetMapping("/lis-by-parent")
    @ApiOperation("根据父类类别查询子级数据列表数据")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryListByParentIdVO>> listByParentId(Long parentId) {
        List<CategoryListByParentIdVO> categoryListByParentIdVOS = categoryService.listByParentId(parentId);
        return JsonResult.ok(categoryListByParentIdVOS);
    }

    @GetMapping("")
    @ApiOperation("类别列表")
    public JsonResult<List<CategoryListItemVO>> list(){
        List<CategoryListItemVO> list = categoryService.list();
        return JsonResult.ok(list);
    }

}
